package com.buba.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.service.OutwarehousingService;
import com.buba.service.WareDetailsService;
import common.R;
import entity.sparemanage.Outwarehousing;
import entity.sparemanage.WareDetails;
import entity.sparemanage.Warehousing;
import entity.sparemanage.dto.insertOutWareDto;
import entity.sparemanage.dto.warehouseDto;
import entity.sparemanage.vo.selAllOutWareHouseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import utils.PageResult;

import java.util.List;

/**
 * <p>
 * 出库记录表 前端控制器
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/outwarehousing")
public class OutwarehousingController {

    @Autowired
    private OutwarehousingService outwarehousingService;

    @Autowired
    private WareDetailsService wareDetailsService;

    /* *
     * @Author: beisheng
     * @Descriprion: 新增出库记录及出库备件明细
     * @Date: 2023/5/31 14:02
     * @Return: common.R
     * @Param: [insertOutWareDto]
     */

    @PostMapping("/insertOutWareDto")
    public R insertOutWareDto(@RequestBody insertOutWareDto insertOutWareDto){
        outwarehousingService.insertOutWareDto(insertOutWareDto);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 全查模糊分页查出库信息
     * @Date: 2023/5/31 15:31
     * @Return: common.R
     * @Param: [warehouseDto]
     */

    @PostMapping("/selAllOutWareHouse")
    public R selAllOutWareHouse(@RequestBody warehouseDto warehouseDto){
        PageResult list = outwarehousingService.selAllOutWareHouse(warehouseDto);
        return R.success(list);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据ID查询出库记录及出库明细
     * @Date: 2023/5/31 19:24
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/selOneToWare/{id}")
    public R selOneToWare(@PathVariable("id") Integer id){
        insertOutWareDto insertOutWareDto = outwarehousingService.selOneToWare(id);
        return R.success(insertOutWareDto);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改出库记录及出库备件明细
     * @Date: 2023/5/31 19:43
     * @Return: common.R
     * @Param: [warehouseDto]
     */

    @PostMapping("/updateOutWare")
    public R updateOutWare(@RequestBody insertOutWareDto warehouseDto){
        outwarehousingService.updateOutWare(warehouseDto);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量删除出库信息
     * @Date: 2023/5/31 19:57
     * @Return: common.R
     * @Param: [ids]
     */

    @PostMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            Outwarehousing warehousing = new Outwarehousing();
            warehousing.setDeleteFlag(1);
            warehousing.setSpId(id);
            outwarehousingService.updateById(warehousing);
            wareDetailsService.remove(new QueryWrapper<WareDetails>().eq("use_id",id));
        }
        return R.success();
    }
}

